<?php
namespace app\index\controller;

use think\Request;

class User extends Common
{
    //my bag(我的书包?不要啥意思)
    public function compare()
    {
        return view();
    }
    //结账
    public function checkout()
    {
        return view();
    }
    //愿望清单
    public function wishlist()
    {
        return view();
    }
    //我的账号
     public function my_account()
     {
        
         // 我推荐的人
         $member = model('member');

         $partners = $member->getAllPartners(session('id'));

//        dump($partners);die; //　所有会员
        $this->assign('partners',$partners);

         $id = session('id');
         $key = 'cascfawfecaesfvesf';
         $key = md5($id.$key);
//        print_r($id);die;
         $text = url('index/log/login','',true,true).'?id='.$id.'&key='.$key;
         $this->assign('text',$text);

        $addressList = db('addresslist')->where('user_id',session('user')[0]['id'])->select();
        $this->assign('address',$addressList);
        $request = Request::instance();
        if($request->isAjax())
        {
            return json(['address'=>$addressList]);
        }
        return view();
     }

     //  二维码  分享链接

    public function qrcode(){
//        header("Content-Type: text/html;charset=utf-8");

        vendor('phpqrcode.phpqrcode');
        $qrcode = new \QRcode();

        $id = session('id');
        $key = 'cascfawfecaesfvesf';
        $key = md5($id.$key);
//        print_r($id);die;
        $text = url('index/log/login','',true,true).'?id='.$id.'&key='.$key;

        $qrcode->png($text,false,'L',4,2);

        die;
    }

    //购物车
    public function cart()
    {
        $cartlist = db('shopcart')->field('a.*,b.tradename,b.img')->alias('a')->join('commodity b','a.ware_id = b.id')->select();
        foreach($cartlist as $key=>$value)
        {
            $cartlist[$key]['sub'] = explode('/',$value['sub']);
            $cartlist[$key]['ware_spec'] = explode('/',$value['ware_spec']);
        }
        $this->assign('care',$cartlist);
        //删除购物车商品
        // echo "<pre>";
        // print_r($cartlist);die;
        $req = Request::instance();
        if($req->isAjax())
        {
            $id = input('post.id','');
            $del = db('shopcart')->where('id',$id)->delete();
            if($del)
            {
                return '1';
            }else
            {
                return '2';
            }
        }
        return view();
    }


}
